package t131446.cbr;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Collection;
import java.util.LinkedList;
import java.util.StringTokenizer;

import jcolibri.cbrcore.CBRCase;
import jcolibri.cbrcore.CaseBaseFilter;
import jcolibri.cbrcore.CaseComponent;
import jcolibri.connector.PlainTextConnector;
import jcolibri.exception.InitializingException;
import jcolibri.util.FileIO;

public class ConectorFichero extends PlainTextConnector{

	public ConectorFichero()
	{
		super();
	}
	
	@Override
	public void close() {
		super.close();
	}

	@Override
	public void deleteCases(Collection<CBRCase> arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void initFromXMLfile(URL arg0) throws InitializingException {
		super.initFromXMLfile(arg0);
	}

	@Override
	public Collection<CBRCase> retrieveAllCases() 
	{
		LinkedList<CBRCase> cases = new LinkedList();
	    try
	    {
	      BufferedReader br = null;
	      br = new BufferedReader(new InputStreamReader(FileIO.openFile(this.PROP_FILEPATH)));
	      String line = "";
	      while ((line = br.readLine()) != null) {
	        if ((!line.startsWith("#")) && (line.length() != 0))
	        {
	          StringTokenizer st = new StringTokenizer(line, this.PROP_DELIM);
	          
	          CBRCase _case = new CBRCase();
	          EstadoPartido estadoQuery = new EstadoPartido();
	          Estrategia estrategiaQuery = new Estrategia();
	          int tokenCont = 0;
	          do
	          {
	        	  String nextToken = st.nextToken();
	        	  System.out.println(nextToken);
	        	  switch(tokenCont)
	        	  {
	        	  	case 0: 
	        	  		{
	        	  			estadoQuery.setId(nextToken);
	        	  			estrategiaQuery.setId(nextToken);
	        	  		}
	        	  	break;
	        	  	case 1: estadoQuery.setMarcador(Integer.parseInt(nextToken));
	        	  	break;
	        	  	case 2: estadoQuery.setTiempoRestante(Long.valueOf((nextToken)));
	        	  	break;
	        	  	
	        	  	case 3: estrategiaQuery.setJugador1(nextToken);
	        	  	break;
	        	  	case 4: estrategiaQuery.setJugador2(nextToken);
	        	  	break;
	        	  	case 5: estrategiaQuery.setJugador3(nextToken);
	        	  	break;
	        	  	case 6: estrategiaQuery.setJugador4(nextToken);
	        	  	break;
	        	  	case 7: estrategiaQuery.setJugador5(nextToken);
	        	  	break;
	        	  }
	        	  tokenCont++;
	          }
	          while (st.hasMoreTokens());
	          _case.setDescription(estadoQuery);
	          _case.setSolution(estrategiaQuery);
	          cases.add(_case);
	        }
	      }
	      br.close();
	    }
	    catch (Exception e)
	    {
	      e.printStackTrace();
	    }
		return cases;
	}

	@Override
	public Collection<CBRCase> retrieveSomeCases(CaseBaseFilter arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void storeCases(Collection<CBRCase> arg0) {
		super.storeCases(arg0);
//		PrintWriter pw = null;
//		Iterator<CBRCase> it = arg0.iterator();
//		while (it.hasNext()){
//	        try
//	        {
//	        	CBRCase c = it.next();
//	        	c.getDescription();
//	            pw = new PrintWriter(fw);
//	 
//	            for (int i = 0; i < 10; i++)
//	                pw.append("Linea " + i);
//	 
//	        } catch (Exception e) {
//	            e.printStackTrace();
//	        } finally {
//	           try {
	           // Nuevamente aprovechamos el finally para
	           // asegurarnos que se cierra el fichero.
////	           if (null != fichero)
////	              fichero.close();
////	           } catch (Exception e2) {
//	              e2.printStackTrace();
//	           }
//	        }
//	        }	
//	        }
	}

}
